import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.bouncycastle.util.encoders.Hex;

public class Test {
	public static final String ENCODING = "UTF-8";
	 public static void main(String[] args) throws Exception {
		
		    String src =  "hello wolrd";
		  
		  
		    System.out.println("*********************SM2************************");
		    SM2Util sm2 = new SM2Util();
		   
	       
	        final ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
	        // 获取一个椭圆曲线类型的密钥对生成器
	        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
	        // 使用SM2参数初始化生成器
	        kpg.initialize(sm2Spec);
	        // 获取密钥对
	        KeyPair keyPair = kpg.generateKeyPair();
	        PublicKey publicKey = keyPair.getPublic();
	        BCECPublicKey p=(BCECPublicKey)publicKey;
	        
	        PrivateKey privateKey = keyPair.getPrivate();
	        BCECPrivateKey s=(BCECPrivateKey)privateKey;
	        
	        String pubKey = Hex.toHexString(((BCECPublicKey) publicKey).getQ().getEncoded(false));         
            
            String priKey  = Hex.toHexString(((BCECPrivateKey) privateKey).getD().toByteArray());          
             
	        pubKey  = "04116176183480c574072af57c6b6c87b404dd9a4465ddcadcd75f7bdf87a721e9d95ba62c57ab2ccc842633f4ca34979d787188f0d1b40691edfbc461bb3e7225";
	        priKey = "f2239272674a62e63ea56b6bca260b2b40bf9d9215e0043ebee092413d77df5c";
	        
            
	        System.out.print("\n公钥："+pubKey);
	 
	      
	        System.out.print("\n私钥："+priKey);
	 
	        
	        
	        String encryptData = sm2.encrypt(pubKey,src);
	        System.out.println("\n加密字符串："+encryptData);
	        
	        //encryptData = "9a1ba40c46cdd4a4a822648039d17e949cd480f3bc03477402dd4775d5836a8688388f1fbd10e5d3436af67a80ac48dbd6d38c62438130fbf33bff871c886c0c52d5e4e7fc375123108bd8cd8a5ae4b808075ab11d9b2078b6e9978ca7f99ceff781f03d6a817955088c56";
	        		 
	 
	        String decryptData= sm2.decrypt(priKey,encryptData);
	        System.out.println("\n解密："+decryptData);
		 
		 
		    System.out.println("*********************SM3************************");
	        String key = "abcdefghj123";
	      
	        String hexStrByKey = SM3Util.encrypt(src+key, key);//签名:dc40375e22df55ffb8b46d9370ab481359d5d278be9b01b0b64a2ac5411d266b
	        System.out.println("带秘钥加密后的密文:" + hexStrByKey);
	        System.out.println("带秘钥明文宇密文校验结果" + SM3Util.verify(src + key, key, hexStrByKey));//验签
 
	        
	        
	        System.out.println("*********************SM4************************");
	        Security.addProvider(new BouncyCastleProvider());

	        src  = "hello,world";
	        key="0123456789abcdef";            //加密密钥，注意必须是128bits，即16个字节
	       
	        byte[] byteResult = SM4Util.encode(src.getBytes(ENCODING), key.getBytes(ENCODING));
	        hexStrByKey = ByteUtils.toHexString(byteResult);
	        System.out.println("SM4带秘钥加密后的密文:" + hexStrByKey);
	        
	        byteResult = SM4Util.decode(byteResult,key.getBytes(ENCODING));
	        hexStrByKey = new String(byteResult);
	        System.out.println("SM4带秘钥解密后的密文:" + hexStrByKey);
	   

	    }
}
